package com.feiclub.subject.infra.basic.utils;
//数据库加密


import com.alibaba.druid.filter.config.ConfigTools;

import java.security.NoSuchAlgorithmException;
import java.security.NoSuchProviderException;

public class DruidEncryptUtil {


    private static  String publicKey;
    private static  String privateKey;

    static {
        try {
            String[] keyPair = ConfigTools.genKeyPair(512);
            privateKey = keyPair[0];
            System.out.println("privateKey:"+privateKey);
            publicKey = keyPair[1];
            System.out.println("publicKey:" + publicKey);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }



//   加密
    public static  String encrypt(String  plainText) throws Exception {
        String encrypt = ConfigTools.encrypt(privateKey, plainText);
        System.out.println("encrypt:" + encrypt);
        return encrypt;
    }


//    解密
    public static  String  decrypt(String encryptText) throws Exception {
        String decrypt = ConfigTools.decrypt(publicKey, encryptText);
        System.out.println("decrypt:"+ decrypt);
        return decrypt;
    }


    public static void main(String[] args) throws Exception {
        String encrypt = encrypt("123456");
        String decrypt = decrypt(encrypt);
        System.out.println("encrypt:" +encrypt);
        System.out.println("decrypt:" +decrypt);
    }
}
